package cn.newgrand.netcall.ui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import org.jivesoftware.resource.Res;
import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.component.VerticalFlowLayout;
import org.jivesoftware.sparkimpl.settings.local.SettingsManager;

public class CloseConfirmFrame extends JPanel {
	
	private JDialog dialog;
	
	//private JLabel label;
	private JRadioButton radio1;
	private JRadioButton radio2;
	private JCheckBox check;
	private JButton buttonConfirm;
	private JButton buttonCancel;
	private ButtonGroup group;
	
    private JPanel generalPanel = new JPanel();
    private JPanel generalPanel2 = new JPanel();
    
	public CloseConfirmFrame(){
		//label = new JLabel("关闭主窗口时:");
		radio1 = new JRadioButton("隐藏到任务通知区域，不退出程序");
		radio2 = new JRadioButton("退出程序");
		group =new ButtonGroup();
		check = new JCheckBox("不再提示");
		buttonConfirm = new JButton("确定");
		buttonCancel = new JButton("取消");
//		GridBagLayout layout = new GridBagLayout();
		this.setLayout(new VerticalFlowLayout());
		this.add(generalPanel);
		this.add(generalPanel2);
//		this.add(label, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,GridBagConstraints.WEST, GridBagConstraints.NONE,new Insets(5, 5, 5, 5), 0, 0));
//		this.add(radio1, new GridBagConstraints(1, 1, 2, 1, 0.0, 0.0,GridBagConstraints.WEST, GridBagConstraints.NONE,new Insets(5, 5, 5, 5), 0, 0));
//		this.add(radio2, new GridBagConstraints(1, 2, 2, 1, 0.0, 0.0,GridBagConstraints.WEST, GridBagConstraints.NONE,new Insets(5, 5, 5, 5), 0, 0));
//		this.add(check, new GridBagConstraints(0, 3, 2, 1, 0.0, 0.0,GridBagConstraints.WEST, GridBagConstraints.NONE,new Insets(5, 5, 5, 5), 0, 0));
//		this.add(buttonConfirm, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0,GridBagConstraints.WEST, GridBagConstraints.NONE,new Insets(5, 5, 5, 5), 0, 0));
//		this.add(buttonCancel, new GridBagConstraints(2, 3, 1, 1, 0.0, 0.0,GridBagConstraints.WEST, GridBagConstraints.NONE,new Insets(5, 5, 5, 5), 0, 0));
		
		group.add(radio1);
		group.add(radio2);
		
        generalPanel.setBorder(BorderFactory.createTitledBorder("关闭窗口时"));
        generalPanel.setLayout(new GridBagLayout());
        generalPanel.add(radio1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,GridBagConstraints.WEST, GridBagConstraints.NONE,new Insets(5, 5, 5, 5), 0, 0));
        generalPanel.add(radio2, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,GridBagConstraints.WEST, GridBagConstraints.NONE,new Insets(5, 5, 5, 5), 0, 0));

		generalPanel2.setLayout(new GridBagLayout());
		generalPanel2.add(check, new GridBagConstraints(0, 0, 2, 1, 0.0, 0.0,GridBagConstraints.WEST, GridBagConstraints.NONE,new Insets(5, 5, 5, 5), 0, 0));
		generalPanel2.add(buttonConfirm, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0,GridBagConstraints.WEST, GridBagConstraints.NONE,new Insets(5, 5, 5, 5), 0, 0));
		generalPanel2.add(buttonCancel, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0,GridBagConstraints.WEST, GridBagConstraints.NONE,new Insets(5, 5, 5, 5), 0, 0));
	}

	public void showCloseConfirmFrame(){
		
		loadSetting();
		dialog = new JDialog(SparkManager.getMainWindow(),"提醒",true);

		buttonConfirm.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				//save
				
				SettingsManager.getLocalPreferences().setDirectWhenClose(group.isSelected(radio2.getModel()));
				SettingsManager.getLocalPreferences().setShowConfirmDialog(!check.isSelected());
				SettingsManager.saveSettings();
				
				dialog.setVisible(false);
				dialog.dispose();
				
        		if(group.isSelected(radio2.getModel())){
                    SparkManager.getMainWindow().shutdown();
        		}else{
        			SparkManager.getMainWindow().saveLayout();
        			SparkManager.getMainWindow().setVisible(false);
        		}
			}
		});
		
		buttonCancel.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				dialog.setVisible(false);
				dialog.dispose();
			}
		});
		
		
		dialog.pack();
		dialog.setSize(300,200);
		dialog.setContentPane(this);
		dialog.setLocationRelativeTo(SparkManager.getMainWindow());
		dialog.setVisible(true);
		dialog.toFront();
	}
	
	private void loadSetting(){

		if(!SettingsManager.getLocalPreferences().isDirectWhenClose()){
			group.setSelected(radio1.getModel(), true);
		}else{
			group.setSelected(radio2.getModel(), true);
		}
	}
}
